package cn.woflcode.game.model;

import cn.woflcode.game.Global.Global;
import cn.woflcode.game.Global.ImageUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 小型敌机
 */
public class SmallEnemyPlane {
    //---------------------状态-----------------
    //位置:x坐标
    private int x;
    //位置:y坐标
    private int y;
    //大小:宽
    private int width;
    //大小:高
    private int height;
    //飞机的图片
    private BufferedImage image = ImageUtil.getImage("enemy_small0");
    //生命数
    private int life=2;
    //击落得分
    private  int score=1;
    //飞行速度
    private int spead=1;
    //小敌机的生命状态
    private Global.FlyObjectState state = Global.FlyObjectState.ALIVE;
    //小敌机的图片
    private BufferedImage[] images={
            ImageUtil.getImage("enemy_small0"),
            ImageUtil.getImage("enemy_small1"),
            ImageUtil.getImage("enemy_small2"),
            ImageUtil.getImage("enemy_small3"),
            ImageUtil.getImage("enemy_small4")
    };
    //定义图片索引
    private int imageIndex=0;

    //---------------------构造器------------------
    public SmallEnemyPlane(){
        this.width=images[0].getWidth();
        this.height=images[0].getHeight();
        //随机取值范围 0~Global.GAME_UI_WIDTH-width
        this.x= ThreadLocalRandom.current().nextInt(0, Global.GAME_UI_WIDTH-width);

        this.y=-height;
    }
    //----------------------功能-----------------
    //画自己
    public void drawSelf(Graphics g){
        g.drawImage(this.getImages(),x,y,null);
    }
    //移动
    public void move(){
        this.y+=spead;
    }
    //开火
    public List<EnemyBullet> fire(){
        List<EnemyBullet> list =new ArrayList<>();
        list.add(new EnemyBullet(x+width/2,y+height));
        return list;
    }
    //爆炸切换图片
    public BufferedImage getImages(){
        //判断当前状态是否是存活状态
        if(state==Global.FlyObjectState.ALIVE){
            return images[0];
        }
        if (state== Global.FlyObjectState.DEATH){
            //当爆炸图片切换为最后一张图片是 DEATH->DELETABLE
            if(images.length-1==imageIndex){
                this.state= Global.FlyObjectState.DELETABLE;
            }else{
                imageIndex++;
                return images[imageIndex];
            }
        }
        return null;
    }
    //判断是否存活
    public boolean isAlive(){
        return this.life>0;
    }
    //判断是否越界
    public boolean outOfBound(){
        return this.y>Global.GAME_UI_HEIGHT;
    }
    //被攻击 生命数-1
    public void attacked(int value){
        this.life-=value;
    }
    //获取小战机外围的圆形的圆心（x,y）和半径
    //圆 x坐标
    public int getCenterX(){
        return x+width/2;
    }
    //圆y坐标
    public int getCenterY(){
        return y+height/2;
    }
    //圆半径
    public int getR(){
        return Math.min(width,height)/2;
    }
    //获取状态的getter  setter方法
    public Global.FlyObjectState getState(){
        return state;
    }
    public void setState(Global.FlyObjectState state){
        this.state=state;
    }
    //获取得分的getter  setter方法
    public int getScore(){
        return score;
    }
    public void setScore(){
        this.score=score;
    }
}
